package com.demo.connectdbtest.module.rest.response;

import com.demo.connectdbtest.module.common.constants.CommonBizStatus;
import com.demo.connectdbtest.module.common.constants.IBizStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 对Page<E>结果进行包装
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class APiResponsePage<B, D> implements Serializable {

    private static final long serialVersionUID = -9155809568358479424L;

    /**
     * 业务状态码
     */
    @ApiModelProperty(value = "业务状态码", required = true, example = "OK")
    private String code;
    /**
     * 业务消息
     */
    @ApiModelProperty(value = "业务消息", required = true, example = "SUCCESS")
    private String message;
    /**
     * 页码
     */
    @ApiModelProperty(value = "页码", required = true)
    private int current;
    /**
     * 页行数
     */
    @ApiModelProperty(value = "页行数", required = true)
    private int pageSize;
    /**
     * 总数
     */
    @ApiModelProperty(value = "总行数", required = true)
    private Long total;    /**
     * 列表数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<B> list;

    /**
     * 附加数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private D extraDate;


    /**
     * 正常响应体，不带 list、data
     *
     * @param <B>
     * @return
     */
    public static <B, D> APiResponsePage<B, D> ok() {
        return new APiResponsePage<>(
                CommonBizStatus.OK.getCode(),
                CommonBizStatus.OK.getMsg(),
                1,
                0,
                0L,
                null,
                null
        );
    }

    /**
     * 正常响应体 不带 data
     *
     * @param <B>
     * @return
     */
    public static <B, D> APiResponsePage<B, D> ok(PageInfo<B> page) {
        return new APiResponsePage<>(
                CommonBizStatus.OK.getCode(),
                CommonBizStatus.OK.getMsg(),
                page.getPageNum(),
                page.getPageSize(),
                page.getTotal(),
                page.getList(),
                null
        );
    }

    /**
     * 正常响应体
     *
     * @param <B>
     * @return
     */
    public static <B, D> APiResponsePage<B, D> ok(PageInfo<B> page, D data) {
        return new APiResponsePage<>(
                CommonBizStatus.OK.getCode(),
                CommonBizStatus.OK.getMsg(),
                page.getPageNum(),
                page.getPageSize(),
                page.getTotal(),
                page.getList(),
                data
        );
    }

    /**
     * 正常响应体，带 list
     *
     * @param list
     * @param <B>
     * @return
     */
    public static <B, D> APiResponsePage<B, D> ok(int current, int pageSize, Long total, List<B> list) {
        return new APiResponsePage<>(
                CommonBizStatus.OK.getCode(),
                CommonBizStatus.OK.getMsg(),
                current,
                pageSize,
                total,
                list,
                null
        );
    }

    /**
     * 正常响应体，带 list、data
     *
     * @param list
     * @param <B>
     * @return
     */
    public static <B, D> APiResponsePage<B, D> ok(int current, int pageSize, Long total, List<B> list, D data) {
        return new APiResponsePage<>(
                CommonBizStatus.OK.getCode(),
                CommonBizStatus.OK.getMsg(),
                current,
                pageSize,
                total,
                list,
                data
        );
    }

    /**
     * 异常响应体，不带data、data
     *
     * @param status
     * @return
     */
    public static <B, D> APiResponsePage<B, D> err(IBizStatus status) {
        return new APiResponsePage<>(
                status.getCode(),
                status.getMsg(),
                1,
                0,
                0L,
                null,
                null
        );
    }

    /**
     * 异常响应体，自定义msg，不list、data
     *
     * @param status
     * @param msg
     * @return
     */
    public static <B, D> APiResponsePage<B, D> err(IBizStatus status, String msg) {
        return new APiResponsePage<>(
                status.getCode(),
                msg,
                1,
                0,
                0L,
                null,
                null
        );
    }

    /**
     * 异常响应体，带list
     *
     * @param status
     * @param <B>
     * @return
     */
    public static <B, D> APiResponsePage<B, D> err(IBizStatus status, List<B> list) {
        return new APiResponsePage<>(
                status.getCode(),
                status.getMsg(),
                1,
                0,
                0L,
                list,
                null
        );
    }

    /**
     * 异常响应体，自定义msg，带list
     *
     * @param status
     * @param msg
     * @param list
     * @param <B>
     * @return
     */
    public static <B, D> APiResponsePage<B, D> err(IBizStatus status, String msg, List<B> list) {
        return new APiResponsePage<>(
                status.getCode(),
                msg,
                1,
                0,
                0L,
                list,
                null
        );
    }

    /**
     * 异常响应体，带list、data
     *
     * @param status
     * @param list
     * @param data
     * @param <B>
     * @param <D>
     * @return
     */
    public static <B, D> APiResponsePage<B, D> err(IBizStatus status, List<B> list, D data) {
        return new APiResponsePage<>(
                status.getCode(),
                status.getMsg(),
                1,
                0,
                0L,
                list,
                data
        );
    }

    /**
     * 异常响应体，自定义msg，带list、data
     */
    public static <B, D> APiResponsePage<B, D> err(IBizStatus status, String msg, List<B> list, D data) {
        return new APiResponsePage<>(
                status.getCode(),
                msg,
                1,
                0,
                0L,
                list,
                data
        );
    }
}
